import http.client
import json,requests
import re


class http_test:
        def test_H(self):
        ###登陆
            c = http.client.HTTPConnection("192.168.8.143",8080)
            c.request("POST","/WoniuSales/user/login",
                      "username=admin&password=admin123&verifycode=0000",
                      {"Content-Type":"application/x-www-form-urlencoded"})
            r = c.getresponse().getheader("Set-Cookie")
            cookie = r.split(";")[0]
            c.close()
            #会员添加
            c = http.client.HTTPConnection("192.168.8.143",8080)
            c.request("POST","/WoniuSales/customer/add",
                      "customername=未和&customerphone=11131118096946&childsex=男&childdate=2018-11-10&creditkids=0&creditcloth=0".encode(),
                     {"Content-Type":"application/x-www-form-urlencoded","charset":"UTF-8","Cookie":cookie})
            r = c.getresponse().read().decode()
            if "add-successful" in r:
                print("test_H01 pass")
            #会员查询
            c.request("POST","/WoniuSales/customer/search",
                      "customerphone=13118096946&page=1".encode(),
                     {"Content-Type":"application/x-www-form-urlencoded","charset":"UTF-8","Cookie":cookie})
            r = c.getresponse().read().decode()
            r1 = json.loads(r)
            r2 = r1[0]["customerphone"]
            if r2 == "13118096946":
                print("test_H02 pass")
            #会员修改
            c.request("POST"," /WoniuSales/customer/edit",
                      "customerid=98&customerphone=13118096946&customername=你二大爷&childsex=女&childdate=2018-11-05&creditkids=900&creditcloth=800".encode(),
                      {"Content-Type":"application/x-www-form-urlencoded","charset":"UTF-8","Cookie":cookie})
            r = c.getresponse().read().decode()
            if "edit-successful" in r:
                print("test_H03 pass")
        def test_S(self):
            se = requests.session()  #实例化session
            ##登陆
            reslo = se.post("http://192.168.8.139:8080/WoniuSales/user/login",
                              {"username":"admin","password":"admin123","verifycode":"0000"})
            #添加
            resadd =se.post("http://192.168.8.139:8080/WoniuSales/customer/add",
                          {"customername":"babb","customerphone":"18408201568","childsex":"男","childdate":"2018-11-10","creditkids":"0","creditcloth":"0"},
                          )

            if re.match("successful",resadd.content):##字符串匹配断言
                print("test_S01 pass")
            #查询
            resquery=se.post("http://192.168.8.143:8080/WoniuSales/customer/search",
                             {"customerphone":"18408201568","page":"1"})
            r = resquery.content.decode()
            r1 = json.loads(r)
            r2 = r1[0]["customerphone"]
            if re.match("18408201568",r2):
                print("test_S02 pass")
            #修改
            resedit = se.post("http://192.168.8.143:8080/WoniuSales/customer/edit",
                             {"customerid":"104","customerphone":"18408201568","customername":"二大爷","childsex":"女","childdate":"2018-11-04","creditkids":"800","creditcloth":"800"})
            r = resedit.content.decode()
            if re.match("edit-successful",r):
                print("test_S03 pass")


            ##cookie的方法
            r = requests.post("http://192.168.8.143:8080/WoniuSales/user/login",
                              {"username":"admin","password":"admin123","verifycode":"0000"})
            print(r.content)
            ##新增
            r1 = requests.post("http://192.168.8.143:8080/WoniuSales/customer/add",
                              {"customername":"未和","customerphone":"18408201568","childsex":"男","childdate":"2018-11-10","creditkids":"0","creditcloth":"0"},
                               r.cookies)
            print(r1.content)
            if re.match(r1.content,"successful"):##字符串匹配断言
                print("test_SC01 pass")
            ##查询
            r2 = requests.post("http://192.168.8.143:8080/WoniuSales/customer/search",
                                {"customerphone":"18408201568","page":"1"},
                                r.cookies)
            r2_1 = r2.content.decode()

